const app = getApp()
var util = require('../../wxParse/util.js');
var utilMd5 = require('../../wxParse/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
        statusBarHeight: getApp().globalData.statusBarHeight,
        statusBarHeight1: getApp().globalData.statusBarHeight1,
        cur:0,
        tc_cur:0,
        currentTab1:0,
        animation: '',
        EventEmitter:[],
        navScrollLeft: 0,
        navScrollLeft1: 0,
        winWidth: 0,
        winHeight: 0, 
        currentTab:0,
        currentTab1:0,
        hidden: true,
        xians:"display:none",
        all_price:"0",
        page:1
  },
      radioChange:function(e){
        this.data.pagee = 1
        app.globalData.zf_zt = e.detail.value
      },
  swicth(e){
    var cur = e.currentTarget.dataset.index;
    if(this.data.cur==cur){
      return false
    }else{
      this.setData({
        cur:cur
      })
    }
  },
  taocan_jl:function(e){
    wx.navigateTo({
      url: '../../pages/taocan_jl/taocan_jl',
    })
  },
  gma:function(e){
    wx.navigateTo({
      url: '../../pages/ht_moban1/ht_moban1',
      })
  },
  menu_jl(){
    wx.navigateTo({
      url: '../menu_jl/menu_jl',
    })
  },
  menu_xq(){
    wx.navigateTo({
      url: '../menu_xq/menu_xq?type=1',
    })
  },
  tc_swidch(e){
    var cur = e.currentTarget.dataset.index;
    if(this.data.tc_cur==cur){
      return false
    }else{
      this.setData({
        tc_cur:cur
      })
    }
  },
  
  switchNav1(event){
    var that = this;
    var cur = event.currentTarget.dataset.current; 
    console.log(cur)
    var singleNavWidth1 = this.data.windowWidth / 5;
    var tu = (cur - 2) * singleNavWidth1
    this.setData({
      navScrollLeft1: (cur - 2) * singleNavWidth1+40
    })      
    if (this.data.currentTab1 == cur) {
      this.setData({
        currentTab1: "999",
    })
        return false;
    } else {
        this.setData({
            currentTab1: cur,
        })
    }
  },

  
  switchNav2(event){
    var that = this;
    this.setData({
      xians:"display:inline-block"
  })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  yu_zf:function(e){
    var that = this;
    that.data.page++;
    if(that.data.page==2){
      if(app.globalData.gl_id==1){
        if(app.globalData.tcid==""){
          wx.showModal({
            title: '',
            content: '请选择购买套餐!',
            showCancel: false,
            success:function(res){
                  if(res.confirm){
                    that.data.page=1
                  }else{
                    
                  }
            }
          })
        }else{
          wx.request({
            url: app.globalData.url_l + 'api/wxpay/gm_taocan', //注意改成自己的服务器请求地址哦！
            data:{
              uid:app.globalData.uid,
              tcid:app.globalData.tcid,
              state:app.globalData.zf_zt
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            success: function (response) {
              console.log(response)
              if(response.data.state=="2"){
                wx.showModal({
                  title: '',
                  content:"支付失败！",
                  showCancel: false,
                  success:function(res){
                        if(res.confirm){
                          that.data.page=1
                        }else{
                           
                        }
                  }
                })
               }else if(response.data.state=="1"){
                wx.showModal({
                  title: '',
                  content: "余额不足！",
                  showCancel: false,
                  success:function(res){
                        if(res.confirm){
                          that.data.page=1
                        }else{
                           
                        }
                  }
                })
               }else if(response.data.state=="0"){
                wx.showModal({
                  title: '',
                  content: "请选择购买模板！",
                  showCancel: false,
                  success:function(res){
                        if(res.confirm){
                          that.data.page=1
                        }else{
                           
                        }
                  }
                })
               }else if(response.data.state=="3"){
                wx.showModal({
                  title: '',
                  content: "支付成功！",
                  showCancel: false,
                  success:function(res){
                        if(res.confirm){
                          wx.switchTab({
                            url: '../wode/wode',
                          })
                          that.data.page=1
                        }else{
                           
                        }
                  }
                })
               }else{
                  var timeStamp = (Date.parse(new Date()) / 1000).toString();
                  var pkg = 'prepay_id=' + response.data.prepay_id;
                  var nonceStr = response.data.nonce_str;
                  var paySign = utilMd5.hexMD5('appId=' + response.data.appid + '&nonceStr=' + nonceStr + '&package=' + pkg + '&signType=MD5&timeStamp=' + timeStamp + "&key=qwertyuiopasdfghjklzxcvbnm123456").toUpperCase();
                  wx.requestPayment({
                      'timeStamp': timeStamp,
                      'nonceStr': nonceStr,
                      'package': pkg,
                      'signType': 'MD5',
                      'paySign': paySign,
                      'success': function (res) {
                        console.log(res)
                       wx.showModal({
                         title: '',
                         content: "支付成功！",
                         showCancel: false,
                         success:function(res){
                               if(res.confirm){
                                 wx.switchTab({
                                   url: '../wode/wode',
                                 })
                                 that.data.page=1
                               }else{
                                  
                               }
                         }
                       })
                       
                      }
                  });
              }
            }
          })
        }
        
      }else{
        wx.showModal({
          title: '',
          content: '只有管理员可以购买套餐!',
          showCancel: false,
          success:function(res){
                if(res.confirm){
                  that.data.page=1
                }else{
                   
                }
          }
        })
      }
    }else{
      this.data.page=1
    }
    
  },
  zhifu:function(e){
    this.setData({
      xians:"display:inline-block"
    })
    

   
  },
  onLoad: function (options) {
    var that = this;
    app.globalData.zf_zt = 0;
    app.globalData.tcid =""
    wx.request({
      url: app.globalData.url_l + 'api/member/mem_yue',
      method: 'post',
      data:{
         uid:app.globalData.uid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
         console.log(res)
         that.setData({
             yue_sj:res.data
         })
      }
    })
    wx.request({
      url: app.globalData.url_l + 'api/member/ht_taocan',
      method: 'post',
      data:{
          uid:app.globalData.uid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        console.log("111111111")
        that.setData({
          toubu:res.data,
          shuzu:res.data.res
        })
        
      }
    })
    wx.getSystemInfo({
      success: (res) => {
          this.setData({
              pixelRatio: res.pixelRatio,
              windowHeight: res.windowHeight,
              windowWidth: res.windowWidth
          })
      },
  }) 
  },

  guanbi:function(e){
    this.setData({
      xians:"display:none",
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})